package com.yanqu.road.server.cmd.unioncampwar;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.unioncampwar.UnionCampWarPatronsMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.concurrent.CompletableFuture;


@Cmd(code = GameProtocol.S_UNION_CAMP_WAR_GET_RANK_REWARD, desc = "领取排行奖励")
public class UnionCampWarGetRankRewardCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        long userId = player.getUserInfo().getUserId();
        UnionCampWar.UnionCampWarGetRankRewardRespMsg.Builder respBuilder = UnionCampWar.UnionCampWarGetRankRewardRespMsg.newBuilder();
        CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToCrossServer(player.getUserId(), CrossProtocol.C3_UNION_CAMP_WAR_GET_REWARD, UnionCampWar.UnionCampWarGetRankRewardReqMsg.newBuilder(), respBuilder);
        future.thenAccept(syncResponse -> {
            if (syncResponse.getErrorCode() != 0) {
                UnionCampWarPatronsMgr.getLogger().error("玩家{},领取商帮排行奖励错误{}", userId, syncResponse.getErrorCode());
                return;
            }
            if (respBuilder.getRet() == 0) {
                player.getModule(CurrencyModule.class).addCurrency(respBuilder.getRewards(), eLogMoneyType.UnionCampWar, eLogMoneyType.UnionCampWarRankReward);
            }
            player.sendPacket(ClientProtocol.U_UNION_CAMP_WAR_GET_RANK_REWARD, respBuilder);
        });


    }

    @Override
    public void sendData(GamePlayer player) {
        if (null == player) {
            return;
        }

    }
}
